import {Component, OnInit, ViewEncapsulation} from '@angular/core';
import {PlanService} from "@train/plan/plan.service";
import {ToolsService} from "@shared/service/tools/tools.service";
import {ActivatedRoute, NavigationExtras, Params, Router} from "@angular/router";
import {AuthService} from "@shared/service/auth/auth.service";
import {TrainTeachInfoVo} from "@interface/index";



@Component({
  selector: 'seed-edit-class-summary',
  templateUrl: './edit-class-summary.component.html',
  styleUrls: ['./edit-class-summary.component.scss'],
  encapsulation: ViewEncapsulation.None,
})
export class EditClassSummaryComponent implements OnInit {
  public teachInfo: TrainTeachInfoVo;
  public type: string;
  public subjectName: string;
  public teacherName: string;
  public summary: string;
  constructor(
    private _planService: PlanService,
    private _toolService: ToolsService,
    private _route: ActivatedRoute,
    private _authService: AuthService,
    private _router: Router
  ) {
    this.teachInfo = new TrainTeachInfoVo();
    this._route.params
      .switchMap((parmas: Params) => {
        this.teachInfo.classScheduleId = parmas['classScheduleId'];
        this.teachInfo['authOrgId'] = this._authService.getAuthOrgId();
        this.fetchTeachInfo(parmas['classScheduleId']);
        return this._route.queryParams;
      }).subscribe((params: Params) => {
      this.type = params['type'];
      this.teachInfo.id = params['id'] || '';
      this.subjectName = params['subjectName'];
      this.teacherName = params['teacherName'];
    });
  }

  /**
   * @description 更新/修改教学信息
   * @author @chenwh576
   * @date 2018/1/3 下午8:56
   */
  updateTeachInfo(): void {
    const query = this.teachInfo;
    this._planService.updateTeachInfo(query)
      .subscribe(res => {
        if (res.result === '0') {
          history.back();
        } else {
          this._toolService.showToast(res.msg);
        }
      });
  }

  /**
   * @description 保存教学信息
   * @author @chenwh576
   * @date 2018/1/3 下午4:40
   */
  saveTeachInfo(): void {
    const query = this.teachInfo;
    this._planService.saveTeachInfo(query)
      .subscribe(res => {
        if (res.result === '0') {
          this.goToTrainDetailPage();
        } else {
          this._toolService.showToast(res.msg);
        }
      });
  }

  /**
   * @description 根据teachInfo.id判断是使用更新函数还是保存函数
   * @author @chenwh576
   * @date 2018/1/6 下午3:40
   */
  handleTeachInfo() {
    if (this.teachInfo.id) {
      this.updateTeachInfo();
    } else {
      this.saveTeachInfo();
    }
  }


  goToTrainDetailPage() {
    const NavigationExtras: NavigationExtras = {
      queryParams: {
        subjectName: this.subjectName,
        teacherName: this.teacherName
      }
    };
    // this._router.navigate(['/train/plan/detail/', this.teachInfo.classScheduleId], NavigationExtras);
    history.back();
  }

  /**
   * @description 设置课后小结
   * @author @chenwh576
   * @date 2018/1/4 下午4:29
   */
  setTeachInfoBySummery(query: string) {
    this.teachInfo.summary = query;
  }

  /**
   * @description 判断是否允许button点击
   * @author @chenwh576
   * @date 2018/1/4 下午4:32
   */
  get isDisabled() {
    if (!this.teachInfo.summary) {
      return true;
    }


    if (this.teachInfo.summary && this.teachInfo.summary.length > 0) {
      return false;
    }

    return true;
  }

  /**
   * @description 根据课时ID 获取教学信息
   * @author ouxh862
   * @date 2018.01.02
   * @param {number} scheduleId
   */
  fetchTeachInfo(scheduleId: number) {
    this._planService.fetchTeachInfo(scheduleId)
      .filter(res => res.result === '0')
      .subscribe(res => {
        this.summary = res.data.summary;
      });
  }
  ngOnInit() {
  }

}
